package com.atguigu.tingshu.handler;

import com.atguigu.tingshu.model.CDCEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * @author zh
 * @date 2025/9/12
 * @description
 */
@Slf4j
@Component
@CanalTable("album_info")
public class AlbumInfoCdcHandler implements EntryHandler<CDCEntity> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void insert(CDCEntity cdcEntity) {
        EntryHandler.super.insert(cdcEntity);
        //直接删除;要么写
    }

    @Override
    public void update(CDCEntity before, CDCEntity after) {
        System.out.println("before:"+before.getId());
        //相当于获取到对应的专辑Id
        System.out.println("after:"+after.getId());
        //删除缓存数据
        String key="album:["+after.getId()+"]";
        try {
            redisTemplate.delete(key);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
    }

    @Override
    public void delete(CDCEntity cdcEntity) {
        EntryHandler.super.delete(cdcEntity);
    }
}
